package com.run.go.gearbox.mysql.codec;

import com.run.go.gearbox.mysql.request.ClientRequest;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufOutputStream;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 * 请求编码处理器
 */
public class NettyMysqlEncoder extends MessageToByteEncoder<ClientRequest> {
    private final static Logger log = LoggerFactory.getLogger(Encoder.class);

    private Encoder encoder = null;

    public NettyMysqlEncoder(Encoder encoder){
        this.encoder = encoder;
    }

    @Override
    public void encode(ChannelHandlerContext ctx, ClientRequest msg, ByteBuf buffer) throws Exception {
        if (log.isDebugEnabled()) {
            log.debug("Sending request: {}", msg);
        }
        ByteBufOutputStream out = new ByteBufOutputStream(buffer);
        encoder.encode(msg, out);
        out.close();
    }
}